/**
 * 
 */
package com.itinsys.enma.dao.impl;

import java.util.List;

import net.sf.ehcache.ObjectExistsException;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;

import com.itinsys.enma.dao.DBTemplate;
import com.itinsys.enma.dao.SchoolDAO;
import com.itinsys.enma.dao.hibernate.DBObject;
import com.itinsys.enma.domain.School;

/**
 * @author pferraz
 *
 */
public class SchoolDAOImpl implements SchoolDAO {

	private static Logger logger=Logger.getLogger("dao");
	/* (non-Javadoc)
	 * @see com.itinsys.enma.dao.ShiftDAO#getById(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public School getById(String schoolId) throws ObjectExistsException {
		List<School> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from s in class com.itinsys.enma.domain.School where s.id=?");
			q.setParameters(new Object[]{schoolId}, new Type[]{Hibernate.STRING});
			result=q.list();
			if(result.isEmpty()){
				throw new ObjectExistsException("La facultad ingresada no existe");	
			}
		}catch(HibernateException e){
			logger.error("Error al buscar facultad por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result.get(0);
	}
	
	
	/* (non-Javadoc)
	 * @see com.itinsys.enma.dao.ShiftDAO#getById(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<School> getAll()  {
		List<School> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from s in class com.itinsys.enma.domain.School order by s.id");
			result=q.list();
			
		}catch(HibernateException e){
			logger.error("Error al buscar la facultad por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result;
	}


	@Override
	public boolean addSchool(School school) {
		
		return DBObject.addObject(school);
	}


	@Override
	public boolean updateSchool(School school) {
		
		return DBObject.updateObject(school);
	}


	@Override
	public boolean deleteSchool(School school) {
		
		return DBObject.removeObject(school);
	}


	@SuppressWarnings("unchecked")
	@Override
	public School getByDesc(String description) {
		List<School> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from s in class com.itinsys.enma.domain.School where s.description=?");
			q.setParameters(new Object[]{description}, new Type[]{Hibernate.STRING});
			result=q.list();
			if(result.isEmpty()){
				throw new ObjectExistsException("La facultad ingresada no existe");	
			}
		}catch(HibernateException e){
			logger.error("Error al buscar facultad por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result.get(0);
	}

}
